package com.doctor.windflower_doctor.activity;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;

import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import com.doctor.windflower_doctor.R;

public class ShowPhotoActivity extends BaseActivity {
	private ImageView show_imageView;
	private ImageButton back;
	private Button modification_button;
	private String filePath;
	private PopupWindow popWindow;
	private View photo_view;
	private ImageView album, take_photo;
	private int CAMERA_RESULT = 5;
	private int PHOTO_RESULT = 1;
	private File imageFile;
	private String imageFilePath;
	private RelativeLayout big_layout;

	@Override
	protected void getSaveData(Bundle savedInstanceState) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void processLogic() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void setListener() {
		// TODO Auto-generated method stub
		take_photo.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Uri imageFileUri = Uri.fromFile(imageFile);
				Intent i = new Intent(
						android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
				i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
						imageFileUri);
				startActivityForResult(i, CAMERA_RESULT);

			}
		});
		album.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				Intent i = new Intent(
						Intent.ACTION_PICK,
						android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);// 调用android的图库
				startActivityForResult(i, PHOTO_RESULT);
			}
		});
		modification_button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				popWindow.showAtLocation(big_layout, Gravity.CENTER, 0, 0);
			}
		});
		back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
	}

	@Override
	protected void findViews() {
		// TODO Auto-generated method stub
		if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
			imageFilePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/mypicture.png";
		}else{
			imageFilePath = getFilesDir().getAbsolutePath() + "/mypicture.png";
		}
		imageFile = new File(imageFilePath);
		photo_view = LayoutInflater.from(ShowPhotoActivity.this).inflate(
				R.layout.takephoto_pop, null);
		big_layout=(RelativeLayout)findViewById(R.id.big_layout);
		Bitmap bitmap = BitmapFactory.decodeFile(imageFilePath);
		show_imageView = (ImageView) findViewById(R.id.show_imageView);
		show_imageView.setImageBitmap(bitmap);
		modification_button = (Button) findViewById(R.id.modification_button);
		back = (ImageButton) findViewById(R.id.back_imageButton);
		album = (ImageView) photo_view.findViewById(R.id.album_imageView);
		take_photo = (ImageView) photo_view
				.findViewById(R.id.takephoto_imageView);
		popWindow = new PopupWindow(photo_view, LayoutParams.MATCH_PARENT,
				LayoutParams.WRAP_CONTENT);
	}

	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		// TODO Auto-generated method stub
		super.onActivityResult(arg0, arg1, arg2);
		if (arg0 == CAMERA_RESULT&&arg1==RESULT_OK) {
			
			// 对拍出的照片进行缩放
			BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
			bmpFactoryOptions.inJustDecodeBounds = true;
			Bitmap bmp = BitmapFactory.decodeFile(imageFilePath,
					bmpFactoryOptions);
			bmpFactoryOptions.inSampleSize = 10;
			bmpFactoryOptions.inJustDecodeBounds = false;
			bmp = BitmapFactory.decodeFile(imageFilePath, bmpFactoryOptions);
			show_imageView.setImageBitmap(bmp);

			popWindow.dismiss();
			BufferedOutputStream bos;
			try {
				bos = new BufferedOutputStream(new FileOutputStream(imageFile));
				bmp.compress(Bitmap.CompressFormat.PNG, 100, bos);
				bos.flush();
				bos.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		} else if (arg0 == PHOTO_RESULT) {
			if(arg2!=null&&arg2.getData()!=null){
				Uri uri = arg2.getData();
				Cursor cursor = ShowPhotoActivity.this.getContentResolver().query(
						uri, null, null, null, null);
				cursor.moveToFirst();
				String imgPath = cursor.getString(1); // 图片文件路径
				cursor.close();
				Options options = new BitmapFactory.Options();
				options.inJustDecodeBounds = false;
				options.inSampleSize = 5;
				Bitmap bitmap = BitmapFactory.decodeFile(imgPath, options);
				show_imageView.setImageBitmap(bitmap);

				popWindow.dismiss();
				BufferedOutputStream bos;
				try {
					bos = new BufferedOutputStream(new FileOutputStream(imageFile));
					bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
					bos.flush();
					bos.close();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			

		}
	}

	@Override
	public int getResLayoutId() {
		// TODO Auto-generated method stub
		return R.layout.showphoto;
	}

}
